﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NCKH.Admin
{
    public partial class QLNNC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hiendonvi();
            }  
        }
        private void hiendonvi()
        {
            string sqlSelect = "SELECT MaDV,TenDV FROM DONVI ";
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlSelect, Cnn))
                {
                    Cnn.Open();
                    using (SqlDataReader Rd = Cmd.ExecuteReader())
                    {
                        ddrtendonvi.Items.Insert(0, "-- Chọn đơn vị --");
                        while (Rd.Read())
                        {
                            ddrtendonvi.Items.Add(
                                new ListItem(Rd["TenDV"].ToString(),
                                    Rd["MaDV"].ToString()));
                        }
                    }
                    Cnn.Close();
                }
            }
        }
        
        
       
        private void timkiemtheotenDV()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("timkiemNNCtheotenDV", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaDV",ddrtendonvi.SelectedValue);
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        da.SelectCommand = Cmd;
                        DataTable tb = new DataTable("DONVI");
                        da.Fill(tb);
                        grvTTNNC.DataSource = tb;
                        grvTTNNC.DataBind();
                    }
                }
            }
        }
        private void timkiemtheotrangthai()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("timkiemNNCtheotranthai", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@Trangthai", ddrtendonvi.SelectedItem.Text);
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        da.SelectCommand = Cmd;
                        DataTable tb = new DataTable("DETAI");
                        da.Fill(tb);
                        grvTTNNC.DataSource = tb;
                        grvTTNNC.DataBind();
                    }
                }
            }
        }

        private void thongbao(string Noidung)
        {

            ScriptManager.RegisterStartupScript(this, this.GetType(), "UpdateCom", "alert('" + Noidung + "');return false;", true);
        }
        protected void btntimkiem_Click(object sender, EventArgs e)
        {
            
                //timkiemtheotenDV();
                timkiemtheotrangthai();
        }
    }
}